package com.cgy.utils.base.fusing;

import com.cgy.utils.base.fusing.handler.FusingHandler;
import com.cgy.utils.base.fusing.setting.FusingCommandSetting;

/**
 * 	熔断设置
 * @author CH
 *
 */
public class Fusing<T> extends FusingHystrix<T> {

	private FusingHandler<T> fusingHandler;

	public Fusing() {
		super(null);
	}
	
	public Fusing(final FusingHandler<T> fusingHandler) {
		super(fusingHandler);
		this.fusingHandler = fusingHandler;
	}
	public Fusing(final FusingHandler<T> fusingHandler, final FusingCommandSetting fusingCommandSetting) {
		super(fusingHandler, fusingCommandSetting);
		this.fusingHandler = fusingHandler;
	}

	public Fusing<T> handler(final FusingHandler<T> fusingHandler) {
		this.fusingHandler = fusingHandler;
		return this;
	}
	
	@Override
	protected T run() throws Exception {
		try {
			return null != fusingHandler ? fusingHandler.execute() : null;
		} catch (Throwable throwable) {
			throwable.printStackTrace();
		}
		return null;
	}

	@Override
	protected T getFallback() {
		if(null == fusingHandler) {
			return null;
		}
		try {
			return fusingHandler.fallback();
		} catch (Exception e) {
			return null;
		}
		
	}

	@Override
	protected String getCacheKey() {
		if(null == fusingHandler) {
			return null;
		}
		return fusingHandler.getCacheKey();
	}

}
